package com.tsu.mall.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tsu.mall.common.VO.LayuiTableVO;
import com.tsu.mall.common.VO.ResultVO;
import com.tsu.mall.common.entity.Member;
import com.tsu.mall.common.mapper.MemberMapper;
import com.tsu.mall.common.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsu.mall.common.util.RequestUtil;
import com.tsu.mall.common.util.StringDbutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tttttt
 * @since 2023-03-22
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public LayuiTableVO doList(Integer page, Integer limit, String memberNumber,String memberPhone) {
        PageHelper.startPage(page,limit);
        List<Member> list=baseMapper.doSelectListPage(memberNumber,memberPhone);
        LayuiTableVO vo = new LayuiTableVO();
        vo.setData(list);
        vo.setCount(new PageInfo<>(list).getTotal());
        return vo;
    }


    @Override
    public LayuiTableVO selectById(Integer id) {
        LayuiTableVO vo=new LayuiTableVO();
        Member member=baseMapper.selectById(id);
        vo.setData(member);
        return vo;
    }

    @Override
    public ResultVO updateByStatus(Integer status,Integer id) {
        ResultVO vo = new ResultVO();
        if (status == 1) {
            String newStatus="启用";
            baseMapper.updateByStatus1(newStatus,id);
        }else {
            String newStatus="停用";
            baseMapper.updateByStatus1(newStatus,id);
        }
        vo.setMsg("执行成功!");
        return vo;
    }

    @Override
    public void updatePassword(Integer id) {
        String password = StringDbutil.md5("123");
        baseMapper.updatePassword(id,password);
    }

    //会员登录判断
    @Override
    public Boolean check(String accountNumber, String password) {
      //redisTemplate.delete("err"+username);
        Object obj=redisTemplate.opsForValue().get("err"+accountNumber);
        if(obj!=null){
            if((int)obj>=5){
                RequestUtil.getSession().setAttribute("msg","密码连续输入错误五次，2小时之后再试！");
            }else {
                RequestUtil.getSession().removeAttribute("msg");
            }
        }
        List<Member> list=baseMapper.check(accountNumber);
        Boolean b= list.size()>0&& list.get(0).getPassword().equals(password);
        if (b){
            RequestUtil.setCurrent(list.get(0));
            redisTemplate.delete("err"+accountNumber);
            return true;
        }else{//密码错误
            if(obj!=null){
                //(int)obj+1
                redisTemplate.opsForValue().set("err"+accountNumber,(int)obj+1,2, TimeUnit.HOURS);
            }else {
                redisTemplate.opsForValue().set("err"+accountNumber,1,2, TimeUnit.HOURS);
            }
        }
        return false;
    }

    @Override
    public Member doSelectById(Integer id) {
       return  baseMapper.selectById(id);
    }

    @Override
    public String selectAccountNumber(String accountNumber,Integer id) {
        return baseMapper.selectAccountNumber(accountNumber,id);
    }

    @Override
    public void updateMemberAvatar(String avatar, Integer id) {
        baseMapper.updateMemberAvatar(avatar,id);
    }

    @Override
    public String selectCurrentAvatar(Integer id) {
        return baseMapper.selectCurrentAvatar(id);
    }

    @Override
    public LayuiTableVO updatePasswordByMemberId(Integer id,String prePassword, String newPassword, String confirmPassword) {
        LayuiTableVO vo=new LayuiTableVO();
        String password= baseMapper.selectPasswordById(id);
        if(prePassword=="" || newPassword=="" || confirmPassword==""){
            vo.setCode(1);
            vo.setMsg("密码信息不全!");
        }else if(!StringDbutil.md5(prePassword).equals(password)){
            vo.setCode(1);
            vo.setMsg("原密码输入错误!");
        }else if(!StringDbutil.md5(confirmPassword).equals(StringDbutil.md5(newPassword))){
            vo.setCode(1);
            vo.setMsg("两次密码输入不一致!");
        }
        else{
            baseMapper.updatePasswordByMemberId(id,StringDbutil.md5(newPassword));
        }
        return vo;
    }

    @Override
    public Member selectPhoneById(String phone) {
        return baseMapper.selectPhoneById(phone);
    }

    @Override
    public LayuiTableVO reg(String code, String username, String phone, String password, String confirmPassword) {
        LayuiTableVO vo = new LayuiTableVO();
        Object obj=redisTemplate.opsForValue().get("reg-code-"+phone);
        //判断code是否正确
        if (obj == null || !obj.equals(code)) {
            vo.setCode(1);
            vo.setMsg("验证码输入不正确");
        }else{
            //判断两次密码是否一致
            if(!password.equals(confirmPassword)){
                vo.setCode(1);
                vo.setMsg("两次密码输入不一致");
            }else{
                //插入数据
                baseMapper.reg(username,phone,StringDbutil.md5(password));
            }
        }
        return vo;
    }


}
